﻿#include "GreatFoxSpirit.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../MapObject.h"
#include "../../Settings.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../MirEnvir/Map.h"
#include "GuardianRock.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool GreatFoxSpirit::getCanMove() const
    {
        return false;
    }

    GreatFoxSpirit::GreatFoxSpirit(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool GreatFoxSpirit::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void GreatFoxSpirit::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        if (Stats[Stat::HP] >= 4)
        {
            unsigned char stage = static_cast<unsigned char>(4 - (HP / (Stats[Stat::HP] / 4)));

            if (stage > _stage)
            {
                _stage = stage;
                Broadcast(GetInfo());
            }
        }

        MonsterObject::ProcessAI();
    }

    void GreatFoxSpirit::Turn(MirDirection dir)
    {
    }

    bool GreatFoxSpirit::Walk(MirDirection dir)
    {
        return false;
    }

    void GreatFoxSpirit::ProcessRoam()
    {
    }

    void GreatFoxSpirit::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }
        //remark: does this mean nobody gets teleported if the main target is standing closeby + does it mean it always try's to teleport the person with lowest x/y coords?)
        if (Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) > 3 && getEnvir()->Random->Next(10) == 0 && getEnvir()->getTime() >= RecallTime)
        {
            RecallTime = getEnvir()->getTime() + 10000;
            std::vector<MapObject*> targets = FindAllTargets(30, getCurrentLocation());
            if (targets.size() != 0 && getEnvir()->Random->Next(4) > 0)
            {
                for (int i = 0; i < targets.size(); i++)
                {
                    if (Functions::MaxDistance(getCurrentLocation(), targets[i]->getCurrentLocation()) > 3)
                    {
                        if (getEnvir()->Random->Next(Settings::MagicResistWeight) < targets[i]->Stats[Stat::MagicResist])
                        {
                            continue;
                        }
                        if (!targets[i]->Teleport(getCurrentMap(), Functions::PointMove(getCurrentLocation(), static_cast<MirDirection>(static_cast<unsigned char>(getEnvir()->Random->Next(7))), 1)))
                        {
                        targets[i]->Teleport(getCurrentMap(), getCurrentLocation());
                        }
                        return;
                    }
                }
            }
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();

            if (getTarget()->Dead)
            {
                FindTarget();
            }

            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }
    }

    void GreatFoxSpirit::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) > 2;

        std::vector<MapObject*> targets = FindAllTargets(ranged ? AttackRange : 2, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        if (ranged)
        {
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            Broadcast(tempVar2);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);
            if (ranged)
            {
                S::ObjectEffect *tempVar3 = new S::ObjectEffect();
                tempVar3->ObjectID = getTarget()->ObjectID;
                tempVar3->Effect = SpellEffect::GreatFoxSpirit;
                Broadcast(tempVar3);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MAC});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GreatFoxSpirit::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (target->Attacked(this, damage, defence) <= 0)
        {
            return;
        }

        PoisonTarget(target, 5, 15, PoisonType::Slow, 1000);
        PoisonTarget(target, 5, 5, PoisonType::Paralysis, 1000);
    }

    void GreatFoxSpirit::Die()
    {
        if (Dead)
        {
            return;
        }
        ChangeGuardianState(false);
        MonsterObject::Die();
    }

    void GreatFoxSpirit::Spawned()
    {
        MonsterObject::Spawned();
        ChangeGuardianState(true);
    }

    void GreatFoxSpirit::ChangeGuardianState(bool Active)
    {
        int ExpectedDistance = 20;
        for (int y = getCurrentLocation()->Y - ExpectedDistance; y <= getCurrentLocation()->Y + ExpectedDistance; y++)
        {
            if (y < 0)
            {
                continue;
            }
            if (y >= getCurrentMap()->Height)
            {
                break;
            }

            for (int x = getCurrentLocation()->X - ExpectedDistance; x <= getCurrentLocation()->X + ExpectedDistance; x++)
            {
                if (x < 0)
                {
                    continue;
                }
                if (x >= getCurrentMap()->Width)
                {
                    break;
                }

                Cell *cell = getCurrentMap()->GetCell(x, y);

                if (!cell->getValid() || cell->Objects.empty())
                {
                    continue;
                }

                for (int i = 0; i < cell->Objects.size(); i++)
                {
                    GuardianRock *target = dynamic_cast<GuardianRock*>(cell->Objects[i]);
                    if (target == nullptr)
                    {
                        continue;
                    }
                    target->Active = Active;
                }
            }
        }
    }

    Packet *GreatFoxSpirit::GetInfo()
    {
        S::ObjectMonster *tempVar = new S::ObjectMonster();
        tempVar->ObjectID = ObjectID;
        tempVar->Name = getName();
        tempVar->NameColour = NameColour;
        tempVar->Location = getCurrentLocation();
        tempVar->Image = Info->Image;
        tempVar->Direction = getDirection();
        tempVar->Effect = Info->Effect;
        tempVar->AI = Info->AI;
        tempVar->Light = Info->Light;
        tempVar->Dead = Dead;
        tempVar->Skeleton = Harvested;
        tempVar->Poison = CurrentPoison;
        tempVar->Hidden = getHidden();
        tempVar->ExtraByte = _stage;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
